#!/bin/bash
# use this script to record each disk, ethernet card speed per second
# and average speed for total
eth0=0
eth1=0
eth2=0
eth3=0
eth[0]=0
eth[1]=0
eth[2]=0
eth[3]=0
eth[4]=0
bond0=0

disk0=0
disk1=0
disk2=0
disk3=0
disk4=0
disk5=0
disk6=0
disk[0]=0;
disk[1]=0;
disk[2]=0;
disk[3]=0;
disk[4]=0;
disk[5]=0;
disk[6]=0;
disktotal=0

file=/tmp/$RANDOM

mkdir -p -f ./splitacc
rm ./splitacc/* -f

cout=0

while : ; do
    let "cout=$cout+1"

    sar -b -d -n DEV 1 1 > $file
    disktotal=`cat $file | head -n 4 | tail -1 | awk '{print $6}'`
    disktotal=`echo "scale=3; $disktotal/2000.0" | bc` 
    echo "$cout $disktotal" >> splitacc/disktotalspeed.acc

    for ((i=7, j=0; i < 14; i++, j++)); do
        disk=`cat $file | head -n $i | tail -1 | awk '{print $5}'`
        disk=`echo "scale=3; $disk/2000.0" | bc`
        disk[$j]=`echo "scale=3; ($disk + ${disk[$j]})" | bc`
        avg=`echo "scale=3; ${disk[$j]} / $cout" | bc`
        echo "$cout $disk" >> splitacc/disk${j}speed.acc
        echo "$cout $avg" >> splitacc/disk${j}avg.acc
    done

    for ((i=17, j=0; i < 22; i++, j++)); do
        net=`cat $file | head -n $i | tail -1 | awk '{print $7}'`
        net=`echo "scale=3; $net/1000.0" | bc` 
        eth[$j]=`echo "scale=3; ($net+ ${eth[$j]})" | bc`
        avg=`echo "scale=3; ${eth[$j]} / $cout" | bc`
        echo $cout "$net" >> splitacc/net${j}speed.acc
        echo $cout "$avg" >> splitacc/net${j}avg.acc
    done
done
